package com.wbdgj.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.wbdgj.R;

import java.text.DecimalFormat;
import java.util.List;

/**
 * ListAdapter
 * Created by XiaoWei on 2015-11-14.
 */
public class TatterTypeAdapter extends BaseListAdapter<LinkedTreeMap> {

    public TatterTypeAdapter(Context context, List<LinkedTreeMap> objects, String id) {
        super(context, objects, id);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            if (getItemViewType(position) == 0) {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.layout_has_no_data, parent, false);
                holder.noDataRootLayout = (LinearLayout) convertView.findViewById(R.id.root_layout);
            } else {
                convertView = ((Activity) (mContext)).getLayoutInflater().inflate(R.layout.ui_tatter_type_item, parent, false);
                holder.typeBg = (LinearLayout) convertView.findViewById(R.id.typeBg);
                holder.title = (TextView) convertView.findViewById(R.id.title);
            }
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        if (hasNoData) {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(getScreenWidth(), getScreenHeight() * 2 / 3);
            holder.noDataRootLayout.setLayoutParams(lp);
        } else {
            final DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
            holder.title.setText(mDataList.get(position).get("rule_title")+"");
            if (id.equals(position+"")){
                holder.typeBg.setBackgroundResource(R.mipmap.tatter_type_true);
                holder.title.setTextColor(mContext.getResources().getColor(R.color.colorTatter));
            }else{
                holder.typeBg.setBackgroundResource(R.mipmap.tatter_type_false);
                holder.title.setTextColor(mContext.getResources().getColor(R.color.colorTatterC));
            }
        }

        return convertView;
    }

    private static final class ViewHolder {
        TextView title;
        LinearLayout typeBg;
        LinearLayout noDataRootLayout;
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.widthPixels;
    }

    private int getScreenHeight() {
        DisplayMetrics displayMetric = Resources.getSystem().getDisplayMetrics();
        return displayMetric.heightPixels;
    }

}
